﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using FJASPX.Service;
using FJASPX.Framwork.UI;
using log4net;
using FJASPX.Framwork.Helper;
using FJASPX.Framwork.Constant;
using FJASPX.Domain;

namespace PlugNT.Cms.Web.Member.Product
{
    public partial class Edit : EditPageBase<FJASPX.Domain.Product>
    {
        public IProductService iProductService { get; set; }
        public IProductCategoryService iProductCategoryService { get; set; }
        public ITradeService iTradeService { get; set; }
        #region Properties属性

        public override string PowerCode
        {
            get { return "51f0cdc42e0a4cdc8290f2e87db79d34"; }
        }

        #endregion

        #region Methods方法

        protected override bool FirstCallRead()
        {

            //初始化控件
            this.btn_BReturn = this.btn_Return;
            base.FirstCallRead();
            this.ddl_Category.SetDataSource(iProductCategoryService.GetAll());
            this.ddl_Trade.SetDataSource(iTradeService.GetAll());
            //判断更新还是添加
            if (this.IsUpdate)
            {
                int id = Convert.ToInt32(StringHelper.GetQueryString(Strings.ID));
                this.Model = iProductService.Get(id);
                if (!this.Model.IsNull())
                {
                    this.txt_Price.SetValue(this.Model.Price);
                    this.txt_Name.SetValue(this.Model.Name);
                    this.txt_Description.Text = this.Model.Content;
                    if (!Model.Images.IsEmpty())
                    {
                        this.img_PicD.Src = Model.Images.GetMin().GetResolvedUrl();
                    }
                    this.ddl_Trade.SetValue(this.Model.Type.ToString());
                    this.ddl_Category.SetValue(this.Model.Cid.ToString());
                }
            }
            return true;
        }

        #endregion

        #region Events事件

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.IsUpdate)
            {
                int id = Convert.ToInt32(StringHelper.GetQueryString(Strings.ID));
                this.Model = iProductService.Get(id);
            }
            else
            {
                this.Model = new FJASPX.Domain.Product();
                this.Model.Creator = this.CurrentUser.UserName;
                this.Model.TimeStamp = DateTime.Now;
                this.Model.UserId = 0;
                this.Model.UserName = this.CurrentUser.UserName;
                this.Model.State = 1;
            }

            this.Model.Name = this.txt_Name.GetValue();
            this.Model.Content = this.txt_Description.Text;
            this.Model.Price = this.txt_Price.GetValue();
            this.Model.Type = Convert.ToInt32(this.ddl_Trade.GetValue());
            this.Model.Cid = Convert.ToInt32(this.ddl_Category.GetValue());
            if (!this.txt_file.GetValue().IsEmpty())
            {
                this.Model.Images = this.SavePic(this.Model.Images, this.txt_file.GetValue(), Cfg.PRODUCT_PAGE_PATH);
            }
            try
            {
                if (!this.Model.Id.IsNull())
                    iProductService.Update(this.Model);
                else
                    iProductService.Save(this.Model);

                //这里加清缓存操作
                this.Success();
                return;
            }
            catch (Exception ex)
            {
                Log.WriteLog(Message.ERR, ex);
            }
            this.Fail();
        }

        #endregion
    }
}